package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;

import ue.UE;
import data.Data;
import eNodeB.Fabrique;
import eNodeB.RessourceBlock;

public class DataPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private Map<UE, List<RessourceBlock>> mapRessourceBlocks;
	private List<Data> datas = new ArrayList<Data>();
	private int[][] table;
	private final Map<Integer, Color> colors = new HashMap<Integer, Color>();

	public DataPanel(int height) {
		updateTable();
		this.setPreferredSize(new Dimension(150, height));
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		this.removeAll();
		colors.clear();
		updateDatas();
		int width = this.getWidth();
		int height = this.getHeight();

		/*
		 * Draw background
		 */
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);

		/*
		 * Draw the ressource blocks in the correct color
		 */
		if (Fabrique.getFabrique().getEnodeB().getRessourceBlockMapping()
				.size() != 0) {
			updateTable();
			Graphics2D g2d = (Graphics2D) g;
			int w = width / 10;
			int h = height / 20;
			int i = 0;
			int j = 0;

			for (i = 0; i < Fabrique.getFabrique().getEnodeB()
					.getMaxRessourceBlocks() / 10; i++) {
				for (j = 0; j < 10; j++) {
					g2d.setColor(colors.get(table[i][j]));
					g2d.fillRect(j * w, i * h, w, h);
				}
			}
			for (j = 0; j < Fabrique.getFabrique().getEnodeB()
					.getMaxRessourceBlocks() % 10; j++) {
				g2d.setColor(colors.get(table[i][j]));
				g2d.fillRect(j * w, i * h, w, h);
			}
		}
		/*
		 * Draw the grid
		 */
		g.setColor(Color.BLACK);
		for (int i = 0; i < 9; i++) {
			g.drawLine((i + 1) * (width / 10), 0, (i + 1) * (width / 10),
					height / 2);
			for (int j = 0; j < 10; j++) {
				g.drawLine(0, (j + 1) * (height / 20), width, (j + 1)
						* (height / 20));
			}
		}
		if (datas != null) {
			int i = 1;
			for (Data d : datas) {
				JLabel jl = new JLabel("For " + d.getUE().toString() + " : "
						+ d.getData());
				jl.setBounds(0, height / 2 + 20 * i, width, 20);
				this.add(jl);
				i++;
			}
		}
	}

	public synchronized void updateTable() {
		table = new int[10][10];
		try {
			Fabrique.getFabrique().getEnodeB().getSem().acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		mapRessourceBlocks = Fabrique.getFabrique().getEnodeB()
				.getRessourceBlockMapping();

		for (UE ue : mapRessourceBlocks.keySet()) {
			List<RessourceBlock> listRB = mapRessourceBlocks.get(ue);
			for (RessourceBlock b : listRB) {
				table[b.getId() / 10][b.getId() % 10] = ue.getId();
			}
			colors.put(ue.getId(), ue.getColor());
		}

		Fabrique.getFabrique().getEnodeB().getSem().release();
	}

	public synchronized void updateDatas() {

		try {
			Fabrique.getFabrique().getEnodeB().getSem().acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		datas = Fabrique
				.getFabrique()
				.getEnodeB()
				.getDatas(
						Fabrique.getFabrique().getEnodeB().getHorloge()
								.getactualTime());

		Fabrique.getFabrique().getEnodeB().getSem().release();
	}
}
